<script setup lang="ts">
import { ref } from 'vue';
import MyTable from './components/MyTable.vue';

// 定义类型并 export 导出
export interface Person {
  id: number
  name: string
  age: number
}

const list = ref<Person[]>([
  { id: 1, name: '张小花', age: 18 },
  { id: 2, name: '孙大明', age: 19 },
  { id: 3, name: '刘德忠', age: 17 },
])

const list2 = ref([
  { id: 1, name: '赵小云', age: 18 },
  { id: 2, name: '刘蓓蓓', age: 19 },
  { id: 3, name: '姜肖泰', age: 17 },
])
// 删除某一条数据
const handleDelete = (id: number) => {
  // 通过 id 进行删除数组中的 对应项 → filter(不会改变原数组，需重新赋值)
  list.value = list.value.filter(v => v.id !== id)
}

const handleDelete2 = (id: number) => {
  // 通过 id 进行删除数组中的 对应项 → filter(不会改变原数组，需重新赋值)
  list2.value = list2.value.filter(v => v.id !== id)
}
</script>

<template>
  <MyTable :data="list">

    <template #default="{id}">
      <button @click="handleDelete2(id)">删除</button>
    </template>

  </MyTable>

  <MyTable :data="list2">

    <template #default="obj">
      <button @click="handleDelete(obj.id)">删除</button>
    </template>

  </MyTable>
</template>
